//
//  BaseProgressView.swift
//  FromWatchToCircle
//
//  Created by 张海峰 on 2018/6/1.
//  Copyright © 2018年 张海峰. All rights reserved.
//
/*尊重OC原创作者：高少东 https://github.com/gsdios/SDProgressView.git */
/*写该demo的目的：主要是想从时钟开始，一步步窥见项目中和圆有关的东西，从而学习巩固，加深理解。
 (项目不定时更新，所以最好从下面地址获取最新demo),感觉我这个demo对你有启发或者帮助，欢迎fork 感谢star
 https://github.com/FighterLightning/FromWatchToCircle.git
 */
import UIKit
let ProgressViewItemMargin: CGFloat = 10
class BaseProgressView: UIView {
    //默认字体大小
    var defaultTextFont: CGFloat = 18
    //默认宽高
    var defaultWH: CGFloat = 100
    //进度默认0
    var progress: CGFloat = 0.0{
        didSet{
            if (progress >= 1.0) {
               self.removeFromSuperview()
            } else {
                self.setNeedsDisplay()
            }
        }
    }
    //默认frame 屏幕中央
    func initBaseProgressView(frame: CGRect) -> UIView {
        defaultWH = frame.size.width
        self.frame = frame
        self.backgroundColor = UIColor.lightGray
        return self
    }
    func setCenterProgressText(text: String,attributes:  [NSAttributedStringKey : Any]){
        //判断系统
        if #available(iOS 7.0, *) {
            //高于 iOS 7.0
            let strSize : CGSize = text.size(withAttributes:  attributes)
            let strX: CGFloat = defaultWH/2 - strSize.width * 0.5
            let strY: CGFloat = defaultWH/2 - strSize.height * 0.5
            //绘制在指定区域
           // (text as NSString).draw(in: self.bounds, withAttributes: attributes)
            //从指定点开始绘制
            (text as NSString).draw(at: CGPoint.init(x: strX, y: strY), withAttributes: attributes)
        } else {
            //低于 iOS 7.0 的版本
            var strSize: CGSize = CGSize()
            var attrStr: NSAttributedString = NSAttributedString()
            if attributes[NSAttributedStringKey.font] != nil{
                strSize = (text as NSString).size(withAttributes: attributes[NSAttributedStringKey.font] as? [NSAttributedStringKey : Any])
                attrStr = NSAttributedString.init(string: text, attributes: attributes)
            }
            else{
                strSize = (text as NSString).size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: defaultTextFont)])
                attrStr = NSAttributedString.init(string: text, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: defaultTextFont)])
            }
            let strX: CGFloat  = (defaultWH - strSize.width)/2
            let strY: CGFloat  = (defaultWH - strSize.height)/2
            attrStr.draw(at: CGPoint.init(x: strX, y: strY))
        }
    }
}
